package yxy.game.pm2.bean;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Map;

import yxy.apple.util.RandomUtility;
import yxy.apple.util.StringUtillity;
import yxy.cherry.data.DataCenter;
import yxy.cherry.data.bean.name;
import yxy.game.pm2.bean.area.AreaBean;
import yxy.game.pm2.bean.area.AreaGroupBean;
import yxy.game.pm2.bean.db.dao.MySQLPlayerDAO;
import yxy.game.pm2.bean.db.module.player;
import yxy.game.pm2.bean.module.BaseGameRedisBean;

public class Nickname extends BaseGameRedisBean {
	static Nickname instance;

	public static Nickname getInstance() {
		if (instance == null) {
			instance = new Nickname();
		}
		return instance;
	}

	private Nickname() {
		init();
	}

	private ArrayList<String> 姓;
	private ArrayList<String> 名;

	private void init() {
		Map<String, name> allnames = DataCenter.getAllData(name.class);
		姓 = new ArrayList<>();
		名 = new ArrayList<>();
		for (name data : allnames.values()) {
			if (data.getType() == 0) {
				姓.add(data.getName());
			} else {
				名.add(data.getName());
			}
		}
	}

	// func==========================================================================
	/** 从名字库里随机一个名字 */
	public String randomName() {
		return String.format("%s %s", //
				姓.get(RandomUtility.nextInt(0, 姓.size())), //
				名.get(RandomUtility.nextInt(0, 名.size())));
	}

	/** 随机一个未使用的名字 */
	public String randomNameNotReuse() {
		String name = randomName();
		while (checkNameIsUsed(name)) {
			name = randomName();
		}
		return name;
	}

	/** 查看名字是否已使用 */
	public boolean checkNameIsUsed(String name) {
		// redis
//		try {
//			return touch().containsKey(name);
//		} catch (RedisException e) {
//		}

		// dao
		try {
			return MySQLPlayerDAO.checkNameIsUsed(name);
		} catch (Exception e) {
			e.printStackTrace();
		}

		return true;
	}

	/** 通过名字查找使用这个名字的玩家code */
	public String getCodeByName(String name) {
		// redis
//		try {
//			return touch().get(name);
//		} catch (RedisException e) {
//			e.printStackTrace();
//			return null;
//		}

		// dao
		try {
			return MySQLPlayerDAO.getByName(name).getCode();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	/** 名字初步整形 */
	public String checkName(String name) {
		name = StringUtillity.toDBC(name).trim().replaceAll(" ", "");
		if (name.length() <= 0) {
			return null;
		}
		return name;
	}

	/** 通过关键词模糊查找玩家：根据专服id、战区id */
	static public Collection<String> search(int clusterId, int areaGroupId, String keyword, int size) {
		ArrayList<String> result = new ArrayList<>();

		// redis
//		try {
//			Set<Entry<String, String>> values = touch().readAllEntrySet();
//			for (Entry<String, String> entry : values) {
//				if (entry.getKey().contains(keyword)) {
//					result.add(entry.getValue());
//				}
//				if (size > 0 && result.size() >= size) {
//					break;
//				}
//			}
//		} catch (RedisException e) {
//			e.printStackTrace();
//		}

		// dao
		try {
			ArrayList<String> serverIds = new ArrayList<>();
			if (areaGroupId != 0) {
				AreaGroupBean areaGroupBean = AreaGroupBean.get(areaGroupId);
				areaGroupBean.getAreaList().forEach(areaId -> {
					AreaBean areaBean = AreaBean.get(Integer.parseInt(areaId));
					serverIds.addAll(areaBean.getServerList()); //战区的所有服
				});

			}
			Collection<player> search = MySQLPlayerDAO.search(clusterId, serverIds, keyword, size);
			for (player db : search) {
				result.add(db.getCode());
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

		return result;
	}

	// redis==========================================================================

	static public String key() {
		return "NAME:PLAYER";
	}

//	static private RMap<String, String> touch() throws RedisException {
//		return RedisManager.client(redis()).getMap(key());
//	}

	public void set(PlayerBean player) {
		// redis
//		try {
//			touch().put(player.getName(), player.getCode());
//		} catch (RedisException e) {
//			e.printStackTrace();
//		}
		// dao
		try {
			MySQLPlayerDAO.setName(player.getCode(), player.getName());
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	// public void remove(String name) {
//		try {
//			touch().remove(name);
//		} catch (Exception e) {
//			e.printStackTrace();
//		}
//	}

	// lock==========================================================================

	@Override
	public String lockKey() {
		return String.format("lock:%s", key());
	}

}
